<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html lang="en">
<head>
    <%@include file="/WEB-INF/jspf/meta/meta.jspf" %>
    <title>Title</title>
    <%@include file="/WEB-INF/jspf/adminPage.jspf" %>
    <script type="text/javascript" src="<%=request.getContextPath()%>/static/schoolRoll/schoolRoll.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath()%>/static/_framework/jquery/jquery.serializejson.js"></script>
    <script type="text/javascript" src="<%=request.getContextPath()%>/static/_framework/jquery/date.js"></script>
    <script>

        //提交的方法(路径)
        var method = '';
        $(function(){
            $('#grid').datagrid({
                url : path_home+'/'+name+'/listByPage',
                striped : true,//隔行换色
                pagination : true,//分页工具栏
                pagePosition : 'bottom',
                pageSize : 10,
                pageNumber : 1,
                pageList : [ 10,20 ],
                columns : columns,//表格字段
                toolbar : [
                    <c:if test="${code.contains('schoolRoll:insert')}">
                    {
                        iconCls : 'icon-add',
                        text : '添加'+text,
                        handler : function() {
                            //点击新增按钮时修改方法名称
                            method = 'add';
                            $('#editDlg').dialog('open');
                            //清空表单
                            $('#editForm').form('clear');
                        }
                    },
                    </c:if>
                    <c:if test="${code.contains('schoolRoll:delete')}">
                    {
                        iconCls : 'icon-remove',
                        text : '删除选中记录',
                        handler : function() {
                            deleteByids();
                        }
                    }
                    </c:if>
                ]
            });
            $('#btnSearch').bind('click',function(){
                var formData = $('#searchForm').serializeJSON();
                $('#grid').datagrid('load',formData);
            });
            /* 编辑窗口 */
            $('#editDlg').dialog({
                title: '编辑窗口',
                width: 300,
                closed: true,
                modal: true
            });

        });


        /**
         * 提交表单
         * @returns
         */
        function submitForm(){
            //判断是否通过校验
            var isValid = $("#editForm").form('validate');
            if (isValid) {
                var formData = $('#editForm').serializeJSON();
                formData['schoolRollNafe']= parseInt(formData['schoolRollNafe']*100)
                formData['schoolRollStudentNafe']= parseInt(formData['schoolRollStudentNafe']*100)
                $.ajax({
                    url:path_home+'/'+name+'/'+method,
                    data:formData,
                    dataType:'json',
                    type:'post',
                    success : function(data) {
                        //返回的数据
                        $.messager.show({
                            title:'提示',
                            msg:data.msg,
                            timeout:3000,
                            showType:'slide'
                        });
                        if(data.code>0){
                            //成功则关闭对话框
                            $('#editDlg').dialog('close');
                            //重新加载表单
                            $('#grid').datagrid('reload');
                        }
                    }
                });
            }else{
                $.messager.alert('提示','请输入正确的信息！','info');
                return ;
            }

        }

        /**
         * 删除选中记录
         */
        function deleteByids(){
            let rows = $("#grid").datagrid("getSelections");
            if (rows.length==0){
                $.messager.alert('提示','请选择行！','info');
                return;
            }
            $.messager.confirm('确认对话框', '您确定要删除吗?', function(r){
                if (r){
                    let ids = rows.map(row=>row.schoolRollId).join(",");
                    $.ajax({
                        url:path_home+'/'+name+'/deleteByIds',
                        data:{'ids':ids},
                        dataType:'json',
                        type:'post',
                        success:function(data){
                            //返回的数据
                            $.messager.show({
                                title:'提示',
                                msg:data.msg,
                                timeout:3000,
                                showType:'slide'
                            });
                            //重新加载
                            $('#grid').datagrid('reload');
                        }
                    });
                }
            });

        }


        /**
         * 格式化价格
         * @param val
         * @returns {*}
         */
        function formatPrice(val) {
            if(typeof val === 'string'){
                if(isNaN(val)){
                    return null;
                }
                // 价格转为整数
                const index = val.lastIndexOf(".");
                let p = "";
                if(index < 0){
                    // 无小数
                    p = val + "00";
                }else if(index === p.length - 2){
                    // 1位小数
                    p = val.replace("\.","") + "0";
                }else{
                    // 2位小数
                    p = val.replace("\.","")
                }
                return parseInt(p);
            }else if(typeof val === 'number'){
                if(val == null){
                    return null;
                }
                const s = val + '';
                if(s.length === 0){
                    return "0.00";
                }
                if(s.length === 1){
                    return "0.0" + val;
                }
                if(s.length === 2){
                    return "0." + val;
                }
                const i = s.indexOf(".");
                if(i < 0){
                    return s.substring(0, s.length - 2) + "." + s.substring(s.length-2)
                }
                const num = s.substring(0,i) + s.substring(i+1);
                if(i === 1){
                    // 1位整数
                    return "0.0" + num;
                }
                if(i === 2){
                    return "0." + num;
                }
                if( i > 2){
                    return num.substring(0,i-2) + "." + num.substring(i-2)
                }
            }
        }


        /* 删除方法 */
        function del(uuid){
            $.messager.confirm('确认对话框', '您确定要删除吗?', function(r){
                if (r){
                    $.ajax({
                        url:path_home+'/'+name+'/delete',
                        data:{'id':uuid},
                        dataType:'json',
                        type:'post',
                        success:function(data){
                            //返回的数据
                            $.messager.show({
                                title:'提示',
                                msg:data.msg,
                                timeout:3000,
                                showType:'slide'
                            });
                            //重新加载
                            $('#grid').datagrid('reload');
                        }
                    });
                }
            });
        }
        //修改部门
        function edit(uuid){
            //清空表单
            $('#editForm').form('clear');
            //修改方法
            method = 'update';
            $('#editForm').form('load',path_home+'/'+name+'/get?id='+uuid);
            //加载完成执行事件
            $('#editForm').form({onLoadSuccess:open})
        }
        function open() {
            //数字框,默认为两位小数,so转为整数进行处理
            let money = parseInt($("#schoolRollNafe").numberbox("getValue"));
            $("#schoolRollNafe").numberbox("setValue",formatPrice(money));
            money = parseInt($("#schoolRollStudentNafe").numberbox("getValue"));
            $("#schoolRollStudentNafe").numberbox("setValue",formatPrice(money));

            $('#editDlg').dialog('open');
        }





        var text='添加学籍';
        var name = "schoolRoll";
        var columns = [ [
            ﻿{field : 'schoolRollId',title: '编号',width: '100px',align : 'center',sortable:true,checkbox:true},
            ﻿{field : 'schoolRollName',title: '学籍种类',width: '100px',align : 'center',sortable:true},
            ﻿{field : 'schoolRollNafe',title: '报读费用',width: '100px',align : 'center',sortable:true,formatter:function(value,row,index){
                    return  formatPrice(value);
            }},
            ﻿{field : 'schoolRollStudentNafe',title: '已纳费用',width: '100px',align : 'center',sortable:true,formatter:function(value,row,index){
                    return  formatPrice(value);
            }},
            ﻿{field : 'schoolRollStyle',title: '学籍状态',width: '100px',align : 'center',formatter:function(value,row,index) {
                return value?'已毕业':'在读';
            }},
            ﻿{field : 'studentId',title: '学生编号',width: '100px',align : 'center'},
        <c:if test="${code.contains('schoolRoll:delete')||code.contains('schoolRoll:update')}">
            {field : 'xxxx',title : '操作',width : '150px',align : 'center',
                formatter : function(value, row, index) {
                return <c:if test="${code.contains('schoolRoll:update')}">"<a href='javascript:void(0)' style='text-decoration: none;' onclick='edit("+row.schoolRollId+")'>修改</a>&nbsp;"</c:if><c:if test="${!code.contains('schoolRoll:update')}">""</c:if> +   <c:if test="${code.contains('schoolRoll:delete')}">"<a href='javascript:void(0)' style='text-decoration: none;' onclick='del("+row.schoolRollId+")'>删除</a>"</c:if><c:if test="${!code.contains('schoolRoll:delete')}">""</c:if>
                }
            }
        </c:if>
        ] ];

        $(function () {
            $('#schoolRollStyle').combobox({
                formatter: function(row){
                    var opts = $(this).combobox('options');
                    return row[opts.valueField]==true?'已毕业':'在读';
                }
            });

            $("#studentId").combobox({
                valueField: 'studentId',
                textField: 'studentName',
                url: path_home+'/student/selectAll',
                editable:false
            })
            $("#schoolRollName").combobox({
                valueField: 'schoolRollName',
                textField: 'schoolRollName',
                editable:false,
                data:[{
                    "schoolRollName":"大专"
                },{
                    "schoolRollName":"本科"
                }]
            })
        })


    </script>
    <style>
        a{
            color: grey;
        }
    </style>
</head>
<body>
<form id="searchForm" method="post" style="margin-bottom: 10px">
    <input class="easyui-textbox" data-options="label: '学籍种类:'" style="width:200px" name="schoolRollName">
    <input class="easyui-textbox" data-options="label: '学籍状态:'" style="width:200px" name="schoolRollStyle">
    <input class="easyui-textbox" data-options="label: '学生编号:'" style="width:200px" name="studentId">

    <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" style="width:80px" id="btnSearch">查询</a>
</form>

<table id="grid"></table>
    <!-- 添加窗口同时也是编辑窗口 -->
    <div id="editDlg">
        <div class="easyui-panel" title="" style="padding:20px;">
            <form id="editForm" method="post" style="margin-top: 10px">
                <div style="margin-bottom:10px">
                    <input class="easyui-combobox" id="schoolRollName" name="schoolRollName" style="width:100%" data-options="label:'学籍种类:',required:true">
                </div>

                <div style="margin-bottom:10px">
                    <input class="easyui-numberbox" id="schoolRollNafe" name="schoolRollNafe" style="width:100%" data-options="precision:2,label:'纳费费用:',required:true">
                </div>
                <div style="margin-bottom:10px">
                    <input class="easyui-numberbox" id="schoolRollStudentNafe" name="schoolRollStudentNafe" style="width:100%" data-options="precision:2,label:'已纳费用:',required:true">
                </div>

                <div style="margin-bottom:10px">
                    <input class="easyui-radiobutton" name="schoolRollStyle" value="false" label="学籍状态:&nbsp;在读:" data-options="labelWidth:'130px'">
                    <input class="easyui-radiobutton" name="schoolRollStyle" value="true" label="&nbsp;&nbsp;已毕业:" data-options="labelWidth:'60px'">

                </div>

                <div style="margin-bottom:10px">
                    <input class="easyui-combobox" id="studentId" name="studentId" style="width:100%" data-options="label:'学生编号:',required:true">
                </div>

                ﻿<input type="hidden" name="schoolRollId">

                <div style="text-align:center">
                    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm()" style="width:80px">保存</a>
                </div>
            </form>
        </div>
    </div>
</body>
</html>